package com.openlab.callsystem.aop;

import com.openlab.callsystem.entity.Caller;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class aspects {
    @Autowired
    RabbitTemplate template;
    //    用Pointcut注解中的属性来指定对哪些方法进行增强
    @Pointcut("@annotation(com.openlab.callsystem.aop.Annotations.Mail)")
    public void send(){}

    /*
        用@Before注解来指定该方法中是增强的代码，并且是在被增强方法执行前执行的
        @Before的属性写上加了@Pointcut注解的方法: 方法名()
    */
    @AfterReturning(value = "send()",returning = "ret")
    public void methodbefore(JoinPoint jp,Object ret){
        Object[] args = jp.getArgs();
        if (ret!=null) {
            Caller caller = (Caller) ret;
            String mailaddress = caller.getEmail();
            template.convertAndSend("mail", args[2]);
        }
    }
}
